<?php
/**
 * Copyright (c) 2008, Julien PORTALIER
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright    Copyright (c) 2008, Julien PORTALIER
 * @link         http://featherphp.googlecode.com/
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Work in Progress.
 */
abstract class ActiveRecord2
{
	protected $_name;
	
	protected $_dbConfig = 'default';
	protected $_dbname;
	protected $_prefix;
	protected $_fields;
	protected $_data;
	
#	protected $_hasMany              = array();
#	protected $_hasOne               = array();
#	protected $_hasOneToMany         = array();
#	protected $_hasAndBelongsToMany  = array();
	
	
	function __construct($data=null)
	{
		$this->_name = get_class($this);
		
		# db handler
		$this->db = Database::get($this->dbConfig);
		
		# database.table
		$this->_dbname = Inflector::tableize($this->_name);
		if (!empty($this->_prefix))
			$this->_dbname = $this->_prefix.$this->_dbname;
		
		# database.table.*
		$this->_fields = memory_cache("active_record/{$this->_dbname}", null, '+1 day');
		if (empty($fields))
		{
			$this->_fields = $this->db->describeTable();
			memory_cache("active_record/{$this->_dbname}", &$this->_fields);
		}
		
		# set data(?)
		if (!empty($data))
		{
			if (is_array($data))
				$this->set($data);
			else
				$this->read($data);
		}
	}
	
	
	// record's data
	
	function __isset($field)
	{
		return isset($this->_data[$field]);
	}
	
	function __get($field)
	{
		return isset($this->_data[$field]) ? $this->_data[$field] : null;
	}
	
	function __set($field, $value)
	{
		if (!array_key_exists($field, $this->_fields))
			trigger_error("No such field '{$this->_dbname}.{$field}'", E_USER_WARNING);
		$this->_data[$field];
	}
	
	
	// read
	
	function read($id=null)
	{
		if (!empty($id))
			$this->id = $id;
		
		$this->data = $this->db->select($this->_dbname, array(
			'where' => array('id' => $this->id),
			'limit' => 1
		));
		
		if (empty($this->data))
			throw new Exception(404, 'No such record.');
	}
	
	function & find($fields='*')
	{
		$result = $this->findAll($fields, 1);
		return isset($result[0]) ? $result[0] : $result;
	}
	
	function & findAll($fields='*', $limit=null, $page=null)
	{
		$result = $this->db->select($this->_dbname, array(
			'limit' => $limit,
			'page'  => $page,
		));
		
		if (!empty($result))
		{
			# we return an array of ActiveRecord
			$class = $this->_name;
			foreach($result as $i => $d)
				$result[$i] = new $class(&$d);
			
			# pagination
			if (!empty($page))
			{
				$result = new ArrayObject($result);
				$result->limit = $limit;
				$result->page  = $page;
				$result->count = $this->findCount();
			}
		}
		return $result;
	}
	
	
	// validate
	
	function validate()
	{
		// ...
	}
	
	
	// save
	
	function create()
	{
		$this->validate();
		$this->db->insert($this->_dbname, $this->_data);
	}
	
	function update()
	{
		$this->validate();
		$this->db->update($this->_dbname, array($this->primaryKey => $id), $this->_data);
	}
	
	
	// delete
	
	function delete()
	{
		$this->db->delete($this->_dbname, $id);
	}
}
?>